// import { http } from "@/utils/http";

// type Result = {
//   success: boolean;
//   data: Array<any>;
// };


import { userKey } from "@/utils/auth";
import { storageLocal } from "@pureadmin/utils";

// ========== 类型定义 ==========
export interface RouteMeta {
  icon: string;
  title: string;
  rank?: number;
  showLink?: boolean;
  keepAlive?: boolean;
  redirect?: string;
  extraIcon?: string;
  enterTransition?: string;
  leaveTransition?: string;
  activePath?: string;
  frameSrc?: string;
  frameLoading?: boolean;
  hiddenTag?: boolean;
  fixedTag?: boolean;
  showParent?: boolean;
}

export interface RouteItem {
  path?: string;
  name?: string;
  component?: string;
  meta: RouteMeta;
  children?: RouteItem[];
}

// 假设后端返回的原始菜单结构
export interface PermissionDto {
  id: string;        // Guid
  parentId?: string; // Guid | null
  path?: string;
  name?: string;
  menuType?: number;   // 用来判断是不是“纯菜单”
  component?: string;
  icon: string;
  title: string;
  sort: number;
  redirect?: string;
  extraIcon?: string;
  enterTransition?: string;
  leaveTransition?: string;
  activePath?: string;
  frameSrc?: string;
  frameLoading?: boolean;
  keepAlive?: boolean;
  hiddenTag?: boolean;
  fixedTag?: boolean;
  showLink?: boolean;
  showParent?: boolean;
}

// ========== 转换器 ==========
export class MenuConverter {
  /** 入口：把扁平的 PermissionDto[] 转成嵌套的 RouteItem[] */
  public static convertToRoutes(menus: PermissionDto[]): RouteItem[] {
    return this.buildRouteTree(null, menus);
  }

  /** 递归建树 */
  private static buildRouteTree(
    parentId: string | null,
    allMenus: PermissionDto[]
  ): RouteItem[] {
    const children = allMenus.filter(m => m.parentId === parentId);
    if (!children.length) return [];

    return children
      .sort((a, b) => b.sort - a.sort) // 降序
      .map(menu => {
        const route: RouteItem = {
          name: menu.name,
          ...(menu.path && { path: menu.path }),
          ...(menu.name && menu.menuType !== 0 && { name: menu.name }),
          ...(menu.component && { component: menu.component }),
          meta: {
            icon: menu.icon,
            title: menu.title,
            redirect: menu.redirect,
            extraIcon: menu.extraIcon,
            enterTransition: menu.enterTransition,
            leaveTransition: menu.leaveTransition,
            activePath: menu.activePath,
            frameSrc: menu.frameSrc,
            frameLoading: menu.frameLoading,
            keepAlive: menu.keepAlive,
            hiddenTag: menu.hiddenTag,
            fixedTag: menu.fixedTag,
            showLink: menu.showLink,
            showParent: menu.showParent
            // rank / showLink / keepAlive 留空即不序列化
          }
        };

        const subChildren = this.buildRouteTree(menu.id, allMenus);
        if (subChildren.length) {
          route.children = subChildren;
        }
        return route;
      });
  }
}

export const getAsyncRoutes = () => {
  /* @ts-ignore */
  const menus = storageLocal().getItem(userKey)?.menus ?? [];
  menus.forEach(item => {
    item.name = item.routeName;
  })
  const flatMenus: PermissionDto[] = menus;
  const routes = MenuConverter.convertToRoutes(flatMenus);

  return {
    success: true,
    data: routes
  };
};

// export const getAsyncRoutes = () => {
//   return http.request<Result>("get", "/get-async-routes");
// };